/**
 * @Title: history-data.js
 * @Description: history-data
 * @author: Fanchao
 * @date: 2021/2/3 11:38 下午
 * @version V1.0
 */
import React, { useEffect, useState } from 'react';
import { connect } from 'umi';
import styles from '@/pages/teaching-quality/class-evaluations/index.less';
import { Button, Input, message, Popconfirm, Table } from 'antd';
import {
    exportAllHistoryEvaluateTeacher,
    exportHistoryStudentEvaluateResult,
} from 'api/index';
import UpdateSourceModal from '@/pages/teaching-quality/class-evaluations/components/update-source-modal';
import DetailModal from '@/pages/teaching-quality/class-evaluations/components/detail-modal';
import * as FileSaver from 'file-saver';

const HistoryData = ({ classEvaluations: { historyDataSource, reqPageInfo, updateSourceFlag, detailData }, dispatch, loading }) => {
    const [center, setCenter] = useState('');
    const [trainingType, setTrainingType] = useState('');
    const [teachSection, setTeachSection] = useState('');
    // const [teacherNo, setTeacherNo] = useState('');
    const [teacherTitle, setTeacherTitle] = useState('');
    const [teacherName, setTeacherName] = useState('');
    const [courseName, setCourseName] = useState('');
    const [show, setShow] = useState(false);
    useEffect(() => {
        dispatch({
            type: 'classEvaluations/getHistoryDataSource',
            payload: {
                center: '',
                trainingType: '',
                teachSection: '',
                // teacherNo: '',
                teacherTitle: '',
                teacherName: '',
                courseName: '',
                reqPageInfo: {
                    pageNumber: 1,
                    pageSize: 10,
                },
            },
        });
    }, []);
    const searchByPageNumber = (pageNumber) => {
        dispatch({
            type: 'classEvaluations/getHistoryDataSource',
            payload: {
                center,
                trainingType,
                teachSection,
                // teacherNo,
                teacherTitle,
                teacherName,
                courseName,
                reqPageInfo: {
                    pageNumber,
                    pageSize: reqPageInfo.pageSize,
                },
            },
        });
    };
    const handleDelete = (item) => {
        dispatch({
            type: 'classEvaluations/deleteHistoryData',
            payload: {
                historyEvaluateTeacherObjectId: item.historyEvaluateTeacherObjectId,
            },
            callback: (result) => {
                if (result) {
                    dispatch({
                        type: 'classEvaluations/getHistoryDataSource',
                        payload: {
                            center,
                            trainingType,
                            teachSection,
                            teacherTitle,
                            // teacherNo,
                            teacherName,
                            courseName,
                            reqPageInfo: {
                                pageNumber: reqPageInfo.pageNumber,
                                pageSize: reqPageInfo.pageSize,
                            },
                        },
                    });
                }
            },
        });
    };
    const exportIte = async (item) => {
        const res = await exportHistoryStudentEvaluateResult({ historyEvaluateTeacherObjectId: item.historyEvaluateTeacherObjectId });
        let blob = new Blob([res.file], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8' });
        FileSaver.saveAs(blob, res.filename);
    };

    const lookDetail = (item) => {
        dispatch({
            type: 'classEvaluations/getDetailData',
            payload: {
                historyEvaluateTeacherObjectId: item.historyEvaluateTeacherObjectId,
            },
        });
        setShow(true);
    };
    const updateSource = (item) => {
        dispatch({
            type: 'classEvaluations/setUpdateSourceFlag',
            payload: true,
        });
        dispatch({
            type: 'classEvaluations/setItemData',
            payload: item,
        });
    };
    const allExport = async () => {
        const res = await exportAllHistoryEvaluateTeacher();
        let blob = new Blob([res.file], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8' });
        FileSaver.saveAs(blob, res.filename);
    };
    // 计算结论判定
    const calcAllEvaluateResult = async () => {
        dispatch({
            type: 'classEvaluations/calcAllEvaluateResult',
            callback: (result) => {
                if (result) {
                    dispatch({
                        type: 'classEvaluations/getHistoryDataSource',
                        payload: {
                            center,
                            trainingType,
                            teachSection,
                            teacherTitle,
                            teacherName,
                            courseName,
                            reqPageInfo: {
                                pageNumber: reqPageInfo.pageNumber,
                                pageSize: reqPageInfo.pageSize,
                            },
                        },
                    });
                }
            },
        });
    };

    const deleteAllEvaluateObjectResult = async () => {
        dispatch({
            type: 'classEvaluations/deleteAllEvaluateObjectResult',
            callback: (result) => {
                if (result) {
                    dispatch({
                        type: 'classEvaluations/getHistoryDataSource',
                        payload: {
                            center,
                            trainingType,
                            teachSection,
                            teacherTitle,
                            teacherName,
                            courseName,
                            reqPageInfo: {
                                pageNumber: reqPageInfo.pageNumber,
                                pageSize: reqPageInfo.pageSize,
                            },
                        },
                    });
                }
            },
        });
    };

    const updateItem = item => {
        message.success(`更新成功`);
    };

    const columns = [
        {
            title: '部系',
            dataIndex: 'center',
            align: 'left',
        },
        {
            title: '教员姓名',
            dataIndex: 'teacherName',
            align: 'left',

        }, {
            title: '职称',
            dataIndex: 'teacherTitle',
            align: 'left',

        },
        {
            title: '课程名称',
            dataIndex: 'courseName',
            align: 'left',
        },
        {
            title: '类别',
            dataIndex: 'trainingType',
            align: 'left',
        },
        {
            title: '教研室',
            dataIndex: 'teachSection',
            align: 'left',
        },
        {
            title: '教学班号',
            align: 'left',
            dataIndex: 'classNo',
        },
        {
            title: '学时',
            dataIndex: 'classHours',
            align: 'left',

        },  {
            title: '有效学员样本',
            dataIndex: 'validStudentSample',
            align: 'left',

        },
        {
            title: '学员评分',
            align: 'left',
            render: (item) => {
                return <span style={{ cursor: 'pointer' }}
                             onClick={() => lookDetail(item)}>{item.studentScore}</span>;
            },

        }, {
            title: '校专家评分',
            align: 'left',
            render: (item) => {
                return <span style={{ cursor: 'pointer' }}
                             onClick={() => updateSource(item)}>{item.schoolExpertScore}</span>;
            },

        }, {
            title: '部系评分',
            align: 'left',
            render: (item) => {
                return <span style={{ cursor: 'pointer' }} onClick={() => updateSource(item)}>{item.centerScore}</span>;
            },

        }, {
            title: '教研室评分',
            align: 'left',
            render: (item) => {
                return <span style={{ cursor: 'pointer' }}
                             onClick={() => updateSource(item)}>{item.teachSectionScore}</span>;
            },
        }, {
            title: '综合得分',
            dataIndex: 'finalScore',
            align: 'left',

        },
        {
            title: '部系排名',
            dataIndex: 'rankInCenter',
            align: 'left',

        },
        {
            title: '判定结论',
            dataIndex: 'evaluateResult',
            align: 'left',

        },
        {
            title: '备注1',
            dataIndex: 'objectType',
            align: 'left',

        },
        {
            title: '备注2',
            dataIndex: 'exemptBasis',
            align: 'left',

        },
        {
            title: '操作',
            align: 'left',
            render: (item) => {
                return (
                    <div>
                        {/*<Button style={{ padding: 2, fontWeight: 'bold' }} type="link"*/}
                        {/*        onClick={() => updateItem(item)}>修改</Button>*/}
                        <Button style={{ padding: 2, fontWeight: 'bold' }} type="link"
                                onClick={() => exportIte(item)}>导出</Button>
                        <Popconfirm title="是否要删除？" onConfirm={() => handleDelete(item)}>
                            <Button style={{ padding: 2, color: '#C21217', fontWeight: 'bold' }}
                                    type="link">删除</Button>
                        </Popconfirm>
                    </div>
                );
            },
        },
    ];


    return (
        <div>
            <div className={styles.filterContainer}>
                <Input
                    className={styles.formWrapper}
                    value={center}
                    onChange={(e) => setCenter(e.target.value)}
                    placeholder="部系"
                />
                <Input
                    className={styles.formWrapper}
                    value={trainingType}
                    onChange={(e) => setTrainingType(e.target.value)}
                    placeholder="类别"
                />
                <Input
                    className={styles.formWrapper}
                    value={teachSection}
                    onChange={(e) => setTeachSection(e.target.value)}
                    placeholder="教研室"
                />
                {/*<Input*/}
                {/*    className={styles.formWrapper}*/}
                {/*    value={teacherNo}*/}
                {/*    onChange={(e) => setTeacherNo(e.target.value)}*/}
                {/*    placeholder="教员编号"*/}
                {/*/>*/}
                <Input
                    className={styles.formWrapper}
                    value={teacherTitle}
                    onChange={(e) => setTeacherTitle(e.target.value)}
                    placeholder="职称"
                />
                <Input
                    className={styles.formWrapper}
                    value={teacherName}
                    onChange={(e) => setTeacherName(e.target.value)}
                    placeholder="教员姓名"
                />
                <Input
                    className={styles.formWrapper}
                    value={courseName}
                    onChange={(e) => setCourseName(e.target.value)}
                    placeholder="课程名称"
                />
                <Button
                    className={styles.marginRight}
                    type="primary"
                    onClick={() => {
                        dispatch({
                            type: 'classEvaluations/getHistoryDataSource',
                            payload: {
                                center,
                                trainingType,
                                teachSection,
                                teacherTitle,
                                // teacherNo,
                                teacherName,
                                courseName,
                                reqPageInfo: {
                                    pageNumber: reqPageInfo.pageNumber,
                                    pageSize: reqPageInfo.pageSize,
                                },
                            },
                        });
                    }}
                >
                    查询
                </Button>
                <Button
                    className={styles.marginRight}
                    onClick={() => {
                        dispatch({
                            type: 'classEvaluations/getHistoryDataSource',
                            payload: {
                                center: '',
                                trainingType: '',
                                teachSection: '',
                                teacherTitle: '',
                                // teacherNo: '',
                                teacherName: '',
                                courseName: '',
                                reqPageInfo: {
                                    pageNumber: reqPageInfo.pageNumber,
                                    pageSize: reqPageInfo.pageSize,
                                },
                            },
                        });
                        setCenter('');
                        setTrainingType('');
                        setTeachSection('');
                        setTeacherTitle('');
                        // setTeacherNo('');
                        setTeacherTitle('');
                        setTeacherName('');
                        setCourseName('');
                    }}
                >
                    重置
                </Button>
                <Button className={styles.marginRight} onClick={calcAllEvaluateResult}>结论判定</Button>
                <Button className={styles.marginRight} onClick={allExport}>导出全部</Button>
                <Popconfirm title="是否要删除？" onConfirm={() => deleteAllEvaluateObjectResult()}>
                    <Button className={styles.marginRight}>删除全部数据</Button>
                </Popconfirm>
            </div>
            <div className={styles.tableContainer}>
                <Table
                    bordered={false} // 是否有边框
                    rowKey='historyEvaluateTeacherObjectId' // 唯一key值
                    loading={loading} // 加载动画
                    columns={columns} // 表头
                    dataSource={historyDataSource.contentList}
                    pagination={{
                        total: historyDataSource.totalElements,// 总记录数
                        defaultPageSize: 10, // 默认显示条数
                        showQuickJumper: true, // 快速跳转
                        showSizeChanger: true, // 一页展示数据条数
                        onChange: (pageNumber) => searchByPageNumber(pageNumber),
                        showTotal: () => {
                            return `共${historyDataSource.totalElements}条`;
                        },
                    }}
                />
            </div>
            <UpdateSourceModal updateSourceFlag={updateSourceFlag}/>
            <DetailModal detailData={detailData} show={show} cb={() => setShow(false)}/>
        </div>
    );
};


export default connect(({ classEvaluations, dispatch, loading }) => ({
    classEvaluations,
    dispatch,
    loading: loading.effects['classEvaluations/getHistoryDataSource'],
}))(HistoryData);
